const TurnHole = function (params) {
    // 笛卡尔 经纬度
    var cartesianTOlongAndlat = function (point) {
        // 将笛卡尔坐标转化为弧度坐标
        var cartographic = Cesium.Cartographic.fromCartesian(point)
        // 将弧度坐标转换为经纬度坐标
        var longitude = Cesium.Math.toDegrees(cartographic.longitude) // 经度
        var latitude = Cesium.Math.toDegrees(cartographic.latitude) // 纬度
        var height = cartographic.height // 高度

        return {
            x: longitude, y: latitude, z: height
        }
    }
    var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    // var entity = viewer.entities.add({
    // 	id: "myEntity",
    // 	position: Cesium.Cartesian3.fromDegrees(114.33, 30.35, 100),
    // 	model: {
    // 		uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
    // 		minimumPixelSize: 128, //模型最小显示尺寸
    // 		maximumScale: 20000,
    // 		color: Cesium.Color.RED,
    // 		colorBlendMode: Cesium.ColorBlendMode.REPLACE //颜色如何与模型混合
    // 	}
    // });

    // // var objectsToExclude = [entity];
    // viewer.zoomTo(entity);


    // var entity1 = viewer.entities.add({
    // 	id: "myEntity1",
    // 	position: Cesium.Cartesian3.fromDegrees(114.33, 30.35, 150),
    // 	model: {
    // 		uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
    // 		minimumPixelSize: 128, //模型最小显示尺寸
    // 		maximumScale: 20000,
    // 		color: Cesium.Color.YELLOW,
    // 		colorBlendMode: Cesium.ColorBlendMode.REPLACE //颜色如何与模型混合
    // 	}
    // });
    // var entity2 = viewer.entities.add({
    // 	id: "myEntity2",
    // 	position: Cesium.Cartesian3.fromDegrees(114.33, 30.35, 200),
    // 	model: {
    // 		uri: 'http://106.12.254.129:39895/data/models3D/d1.gltf',
    // 		minimumPixelSize: 128, //模型最小显示尺寸
    // 		maximumScale: 20000,
    // 		color: Cesium.Color.BLUE,
    // 		colorBlendMode: Cesium.ColorBlendMode.REPLACE //颜色如何与模型混合
    // 	}
    // });
    // viewer.zoomTo(entity1);

    handler.setInputAction(function (clickEvent) {
        // viewer.entities.removeAll()
        var a = viewer.scene._imageryLayerCollection;
        viewer.scene.globe.depthTestAgainstTerrain = false; // 开启深度测试

        //获取地形高度 获取加载地形后对应的经纬度和高程
        var ray1 = viewer.camera.getPickRay(clickEvent.position);
        var cartesian = viewer.scene.globe.pick(ray1, viewer.scene);
        viewer.entities.add({
            position: cartesian,
            point: {
                color: Cesium.Color.RED,
                pixelSize: 10,
                // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
            }
        });

        //获取地形坐标
        const dixing = cartesianTOlongAndlat(cartesian)
        var pick = viewer.scene.pickPosition(clickEvent.position);
        viewer.entities.add({
            position: pick,
            point: {
                color: Cesium.Color.GREEN,
                pixelSize: 10,
                // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
            }
        });
        cartesianTOlongAndlat(pick)
        // 从场景的深度缓冲区中拾取相应的位置，返回笛卡尔坐标
        var pickModel = viewer.scene.pick(clickEvent.position);
        if (pickModel && pick && pickModel.id) {
            var height = Cesium.Cartographic.fromCartesian(pick).height;
            var lat = Cesium.Math.toDegrees(Cesium.Cartographic.fromCartesian(pick).latitude);
            var lng = Cesium.Math.toDegrees(Cesium.Cartographic.fromCartesian(pick).longitude);
            cartesian = Cesium.Cartesian3.fromDegrees(lng, lat, height);
            var cartesian2 = Cesium.Cartesian3.fromDegrees(lng, lat, height + 100);
            cartesianTOlongAndlat(cartesian)
            viewer.entities.add({
                position: cartesian,
                point: {
                    color: Cesium.Color.DARKORANGE,
                    pixelSize: 20,
                    // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
                }
            });
            viewer.entities.add({
                position: cartesian2,
                cylinder: {
                    length: 200,
                    topRadius: 5,
                    bottomRadius: 5,
                    material: Cesium.Color.BLUE
                }
            });


        }
        var drillPick = viewer.scene.drillPick(clickEvent.position, 8, 4, 4);
        // var height = viewer.scene.sampleHeight(Cesium.Cartographic.fromCartesian(pick), pickModel);

        var scene = viewer.scene;
        if (scene.mode !== Cesium.SceneMode.MORPHING) {
            var pickedObject = scene.pick(clickEvent.position);
            if (scene.pickPositionSupported && Cesium.defined(pickedObject)) {
                var cartesian = viewer.scene.pickPosition(clickEvent.position);
                if (Cesium.defined(cartesian)) {
                    var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
                    viewer.entities.add({
                        position: cartographic,
                        point: {
                            color: Cesium.Color.DARKORANGE,
                            pixelSize: 10,
                            // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
                        }
                    });


                    var lng = Cesium.Math.toDegrees(cartographic.longitude);
                    var lat = Cesium.Math.toDegrees(cartographic.latitude);
                    var height = cartographic.height;//模型高度
                }
            }
        }



        // var ens = [];y

        // 求 在模型上的点击的点位置 x,y,z



    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

}

export default TurnHole